import * as log4js from "log4js";

export class LoggerFactory {
    static logger = log4js;
    static {
        const base = './run-logs';
        this.logger.configure({
            appenders: {
                file: {
                    type: 'dateFile',
                    filename: `${base}/access`,
                    pattern: 'yyyy-MM-dd.log',
                    alwaysIncludePattern: true,
                    compress: true,
                    maxLogSize: 10485760,
                    backups: 5,
                    layout: {
                        type: 'pattern',
                        pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] %c - %m%n'
                    }
                },
                console: {
                    // 添加console appender
                    type: 'console',
                    layout: {
                        type: 'colored'
                    }
                }
            },
            categories: {
                default: {appenders: [ 'file', 'console'], level: 'info'}
            }
        })
    }

    public static getLogger(cli?: string): log4js.Logger {
        return this.logger.getLogger(cli);
    }
}